# 模块导入
from . import Category
from . import database


# Product表的mapper
class CategoryMapper:
    # query
    # 根据id查找Category项
    # <Category 1>
    @staticmethod
    def query_category_id(category_id):
        return Category.query.get(category_id)

    # 查找Category列表
    # [<Category 1>, <Category 2>, ...]
    @staticmethod
    def query_all():
        return Category.query.filter().all()

    # 根据大类名称category_name查询（列表）
    # [<Category 1>]
    @staticmethod
    def query_category_name(category_name):
        return Category.query.filter_by(category_name=category_name).all()
        # 同样的，在需要列表的时候，可以使用 .all 方法来获得列表

    # 根据大类名称category_name模糊查询（列表）
    @staticmethod
    def query_category_name_like(keyword):
        return Category.query.filter(Category.category_name.like('%' + keyword + '%')).all()

    # insert
    # 增加一个category项
    @staticmethod
    def insert(category_name):
        category = Category(category_name=category_name)
        database.session.add(category)
        database.session.commit()
        return "insert success"

    # update
    # 更新category项
    @staticmethod
    def update(category_id, category_name, ):
        category = Category.query.get(category_id)
        category.category_name = category_name
        database.session.commit()
        return "update success"

    # delete
    # 删除category项
    @staticmethod
    def delete(id):
        category = Category.query.get(id)
        database.session.delete(category)
        database.session.commit()
        return "delete success"
